package com.wondertek.mist.utils;

import java.io.File;

/**
 * @author wgb
 * 对文件与文件夹删除的处理*/
public class FileUtils {

	public static boolean deleteFile(String sPath) {
		boolean flag = false;
		File file = new File(sPath);		
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}

	public static boolean  deleteDirectory(String sPath) {
		boolean flag = false;		
		if (!sPath.endsWith(File.separator)) {
			sPath = sPath + File.separator;
		}
		File dirFile = new File(sPath);
		if (!dirFile.exists() || !dirFile.isDirectory()) {
			return false;
		}
		flag = true;
		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isFile()) {
				flag = deleteFile(files[i].getAbsolutePath());
				if (!flag)
					break;
			} else {
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
		}
		if (!flag){return false;}
		if (dirFile.delete()) {
			return true;
		} else {
			return false;
		}
	}

}
